package com.beidasoft.web.util.encrypt.factory;

import cn.hutool.core.util.StrUtil;
import com.beidasoft.web.util.encrypt.EncAndDecProperties;
import com.beidasoft.web.util.encrypt.service.BDPEncryptStrategyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author zhangwj1
 * @Description TODO
 * @date 2022/10/9 10:19
 * @Version 1.0
 */
@Service
public class BDPEncryptFactory {
    @Resource
    Map<String, BDPEncryptStrategyService> strategys = new ConcurrentHashMap(5);
    @Resource
    private EncAndDecProperties encAndDecProperties;

    public BDPEncryptFactory() {
    }

    public BDPEncryptStrategyService getEncryptStrategyService(String component) {
        if (StrUtil.isEmpty(component)) {
            component = this.encAndDecProperties.getEncryptType();
        }

        BDPEncryptStrategyService strategy = (BDPEncryptStrategyService)this.strategys.get(component);
        if (strategy == null) {
            throw new RuntimeException("不存在" + component + "类型的加密方式");
        } else {
            return strategy;
        }
    }

    public BDPEncryptStrategyService getEncryptStrategyService() {
        return (BDPEncryptStrategyService)this.strategys.get(this.encAndDecProperties.getEncryptType());
    }
}
